package com.luoqy.speedy.util;

import java.util.ArrayList;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.luoqy.speedy.core.cache.Cache;
import com.luoqy.speedy.core.cache.CacheServiceImpl;

/**
 * @author qf
 * 返回数据自定义
 * message 提示信息
 * state 状态码 500 失败  200成功  状态码同步web请求状态码  若需其他自定义状态请自行定义
 * data 返回数据，若没有返回数据时候，请设置为null
 */
public class Result {
	private String message;
	private int state;
	private Object data;
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public Object getData() {
		if(null==data){
			data=new ArrayList<Map<String,Object>>();
		}
		return data;
	}
	public void setData(Object data) {
		if(null==data){
			data=new ArrayList<Map<String,Object>>();
		}
		this.data = data;
	}
	
	public Result() {
	}
	public Result(int state, String message, Object data) {
		if(null==data){
			data=new ArrayList<Map<String,Object>>();
		}
		CacheServiceImpl result=new CacheServiceImpl();
		Result jsonResult=new Result();
		jsonResult.setData(data);
		jsonResult.setMessage(message);
		jsonResult.setState(state);
		result.putCache("result",jsonResult, 0);
		this.state = state;
		this.message = message;
		this.data = data;
		
	}

	/**
	 * @param message 消息
	 * @param data 数据
	 * @return
	 */
	public static Result success(String message, Object data) {
		if(null==data){
			data=new ArrayList<Map<String,Object>>();
		}
		CacheServiceImpl resultCache=new CacheServiceImpl();
		Result result=new Result(200, message, data);
		resultCache.putCache("result",result, 0);
		return result;
	}

	/**
	 * @param message 消息
	 * @return
	 */
	public static Result error(String message) {
		CacheServiceImpl resultCache=new CacheServiceImpl();
		Result result=new Result(500, message,new ArrayList<Map<String,Object>>());
		resultCache.putCache("result", result, 0);
		return result;
	}
}
